import {defineStore} from 'pinia'
import { getVidoData } from '@/api/search'
import {getshopData} from '@/api/search'
import {getcollectionData} from '@/api/search'
import { showNotify } from 'vant'
import { cancelVideoCollection } from '@/api/api'

export const userSearchStore=defineStore('search',{

  state:()=>({
     videoResult:null,//村视频
     shopResults:null,//购买视频
     collectionData:null,//收藏视频
     userId:JSON.parse(localStorage.getItem('userId')|| '0'),
     sessionId:JSON.parse(localStorage.getItem('sessionId')||'0')

  }),

  actions:{

    async handleSearch(categoryId:string,page:number,count:number){
      try{
         const videoData=await getVidoData({categoryId,page,count})
         console.log('videoData',videoData)

         this.$state.videoResult=videoData.data.result || []
         console.log(this.$state.videoResult,'获取视频')
      }catch(error){
         console.log('获取视频失败')
      }
    },

    //购买视频
    async shopdata(videoId:number,price:number){
      try{
         const {userId,sessionId}=await userSearchStore()
         const shopData=await getshopData(userId,sessionId,{videoId,price})
        

          if(shopData && shopData.data.code===200){
            this.$state.shopResults=shopData.data.message
            console.log(this.$state.shopResults,'买视频')
          }else{
            console.log('购买成功111',shopData.data.message)
          }
      } catch(error){
         console.error('失败购买',error)
      }
    },


    //收藏
    async handshouData(videoId:number){
      try{
          const {userId,sessionId}=await userSearchStore()
          const shopData=await getcollectionData(userId,sessionId,{videoId})
          console.log(shopData,'收藏视频成功')

      }catch(error){
         console.error('收藏失败')
      }
    },

    //取消收藏
    async deleteScllect(videoId:number){
           try{
             const {userId,sessionId}=await userSearchStore()
             const deleteVideoScllect=await cancelVideoCollection(userId,sessionId,{videoId})
             console.log(deleteVideoScllect,'取消收藏12121212')
           }catch(error){
             console.error('取消失败2222')
           }
    }

  },








})

